package com.example.chatweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.chatweb.entity.History;
import com.example.chatweb.mapper.HistoryMapper;
import com.example.chatweb.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;



@Service
public class HistoryServiceImpl implements HistoryService {


    @Autowired
    private HistoryMapper historyMapper;


    @Override
    public void addHistory(History history){
        historyMapper.insert(history);
    }

    public List<History> findHistoriesByTopic(String topic,Integer userid) {
        QueryWrapper<History> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid).eq("topic", topic);
        return historyMapper.selectList(queryWrapper);
    }

    public List<String> findAllTopics(Integer userid){
        QueryWrapper<History> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid).select("DISTINCT topic");
        List<History> histories = historyMapper.selectList(queryWrapper);
        return histories.stream().map(History::getTopic).collect(Collectors.toList());
    }
//    @Autowired
//    public HistoryServiceImpl(HistoryMapper historyMapper) {
//        this.historyMapper = historyMapper;
//    }
//
//    @Override
//    public void insertHistory(History history) {
//        historyMapper.insertHistory(history);
//    }
//
//    public void deleteHistory(long id) {
//        historyMapper.deleteHistoryById(id);
//    }
//
//    @Override
//    public History findHistoryById(Long id) {
//        return historyMapper.selectHistoryById(id);
//    }
//
//    @Override
//    public List<History> findAllHistories() {
//        return historyMapper.selectAllHistories();
//    }
}
